import 'package:flutter/material.dart';
import 'package:flutter_jd/pages/goods/detail/view.dart';
import 'package:flutter_jd/tools/data/mock.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:get/route_manager.dart';
import 'package:get/state_manager.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class CartController extends GetxController {
  final SlidableController slidableController = SlidableController();
  RefreshController refreshController = RefreshController(
    initialRefresh: false,
  );
  final isSelectAll = RxBool(false);
  final totalPrice = RxDouble(0);
  final selectedNumber = RxInt(0);
  List productList = RxList()(cartProductList);
  int currentPage = 1;
  int lastPage = 5;
  List slideButtonList = [
    {
      'name': '设置\n常买',
      'type': '1',
      'textColor': Colors.black,
      'color': [Color(0xFFF0F0F0), Color(0xFFF0F0F0)]
    },
    {
      'name': '移入\n关注',
      'type': '2',
      'textColor': Colors.white,
      'color': [Color(0xFFFEB600), Color(0xFFFFD75A)]
    },
    {
      'name': '看相似',
      'type': '3',
      'textColor': Colors.white,
      'color': [Color(0xFFFF6223), Color(0xFFFF7F46)]
    },
    {
      'name': '删除',
      'type': '4',
      'textColor': Colors.white,
      'color': [Color(0xFFF22300), Color(0xFFFE4614)]
    }
  ];

  @override
  void onInit() {
    super.onInit();
    loadData();
  }

  // 加载数据
  loadData({bool initData = false}) {
    refreshController.loadComplete();
  }

  // 下拉刷新
  onRefresh() async {
    refreshController.loadComplete();
    loadData(initData: true);

    await Future.delayed(Duration(milliseconds: 200));
    refreshController.refreshCompleted();
  }

  // 加载更多
  onLoading() async {
    refreshController.loadNoData();
    await Future.delayed(Duration(milliseconds: 200));

    // if (currentPage < lastPage) {
    //   _loadData();
    // } else if (currentPage == lastPage) {
    //   _refreshController.loadNoData();
    // }
  }

  toDetail(Map item) {
    Get.to(
      () => GoodsDetail(arguments: {
        'imgUrl': item['src'],
        'name': item['name'],
        'price': item['price'],
      }),
    );
  }

  // 计算选中商品数量
  computedProductNumber() {
    // int count = 0;

    // for (var i = 0; i < productList.length; i++) {
    //   var children = productList[i]['children'];

    //   for (var l = 0; l < children.length; l++) {
    //     if (children[l]['isSelected']) {
    //       ++count;
    //     }
    //   }
    // }

    // selectedNumber.value = count;
  }

  // 店铺勾选
  shopClick(int index, bool isSelected) {
    // productList[index]['isSelected'] = !isSelected;

    // // 选中/取消选中店铺下面的商品
    // List children = productList[index]['children'];
    // double allPrice = 0;

    // for (var i = 0; i < children.length; i++) {
    //   if (children[i]['isSelected']) {
    //     totalPrice.value -= children[i]['price'] * children[i]['num'];
    //   }

    //   children[i]['isSelected'] = !isSelected;
    //   allPrice += children[i]['price'] * children[i]['num'];
    // }

    // if (productList[index]['isSelected']) {
    //   totalPrice.value += allPrice;
    // }

    // isSelectAll.value = getSelectedStoreNumber() == productList.length;

    // computedProductNumber();
  }

  // 商品勾选
  goodsClick(Map item, int index, int cIndex) {
    // productList[index]['children'][cIndex]['isSelected'] = !item['isSelected'];

    // int selectedProductNumber = 0;
    // List children = productList[index]['children'];

    // for (var i = 0; i < children.length; i++) {
    //   if (children[i]['isSelected']) {
    //     ++selectedProductNumber;
    //   }
    // }
    // productList[index]['isSelected'] = selectedProductNumber == children.length;

    // isSelectAll.value = getSelectedStoreNumber() == productList.length;

    // bool isSelected = children[cIndex]['isSelected'];
    // int price = children[cIndex]['price'] * children[cIndex]['num'];

    // isSelected ? totalPrice.value += price : totalPrice.value -= price;

    // computedProductNumber();
  }

  // 获取选中店铺数量
  int getSelectedStoreNumber() {
    int count = 0;
    // for (var i = 0; i < productList.length; i++) {
    //   if (productList[i]['isSelected']) {
    //     ++count;
    //   }
    // }
    return count;
  }

  // 全选
  selectAllClick() {
    // isSelectAll.value = !isSelectAll.value;
    // double price = 0;

    // for (var i = 0; i < productList.length; i++) {
    //   var children = productList[i]['children'];

    //   productList[i]['isSelected'] = isSelectAll;

    //   for (var l = 0; l < children.length; l++) {
    //     productList[i]['children'][l]['isSelected'] = isSelectAll;
    //     var childrenItem = productList[i]['children'][l];
    //     price += childrenItem['price'] * childrenItem['num'];
    //   }

    //   totalPrice.value = isSelectAll.value ? price : 0;
    // }

    // computedProductNumber();
  }
}
